import router from './router'
import store from './store'

// 导航守卫
router.beforeEach(async(to, from, next) => {
  const token = store.getters.token
  //   console.log(token)
  const whiteList = ['/404', '/login', '/test']
  if (token) { // 有token
    if (to.path === '/login') {
      next('/')
    } else {
      // 有token且不是去登录页就获取用户信息
      await store.dispatch('user/getUserInfo')
      next()
    }
  } else { // 无token
    if (whiteList.includes(to.path)) { // 在白名单
      next()
    } else { // 无token，不在白名单
      if (to.path !== '/dashboard') next(`/login?redirect=${to.path.slice(1)}`)
      else next('/login')
    }
  }
})
